<template>
    <mi-form-page @submit="submit"
                  title="退款/售后编辑"
                  v-model:show="edit.show"
                  :loading="edit.loading"
    >

        <el-form :model="edit.info"
                 ref="formRef"
                 :rules="rules">

            <el-form-item label="申请类型">
                <el-tag v-if="row.refund_type == 1">仅退款</el-tag>
                <el-tag v-else-if="row.refund_type == 2">退货退款</el-tag>
            </el-form-item>
            <el-form-item prop="audit_status" label="是否同意">
                <mi-radio v-model="edit.info.audit_status" />
            </el-form-item>
            <el-form-item label="退货地址" prop="refund_address_id" v-if="row.refund_type == 2">
                <site-address-select v-model="edit.info.refund_address_id"  @change="refundAddressChange" />
            </el-form-item>
            <el-form-item prop="refund_note" label="拒绝原因">
                <mi-input type="textarea" rows="3" v-model="edit.info.refund_note" />
            </el-form-item>

        </el-form>
    </mi-form-page>
</template>

<script setup lang="ts">
import {reactive, watch, ref, onBeforeMount, inject, computed} from "vue";
import {makeModelAuth} from "@/utils/auth";
import List from "@/model/list";
import Edit from "@/model/edit";
import {FormInstance} from "element-plus";
import {useAdminStore} from "@/store/admin";
import SiteAddressSelect from "@/views/site/components/SiteAddressSelect.vue";

const formRef = ref<FormInstance>()
const auth = makeModelAuth("store.orderRefund")
const rules = {
    audit_status:[{required:true, message:'必填'}],
    refund_note:[
        {
            validator:(rule:any, value:any, callback:any) => {
                if (edit.info.audit_status==0 && !value){
                    callback(new Error('拒绝退款必须填写原因'))
                }
                callback()
            }
        }
    ],
    refund_address_id:[
        {
            validator:(rule:any, value:any, callback:any) => {
                if (row.value.refund_type == 2 && !value){
                    callback(new Error('退货退款必须选择退货地址'))
                }
                callback()
            }
        }
    ],
}
const adminStore = useAdminStore()
const list = inject<List>("list")
const row = computed(() => {
    return list?.currentRow
})
const edit = inject<Edit>("audit", new Edit({}))

const submit = () => {
    edit.info.id = list?.currentRow?.id
    edit.submit({
        formRef: formRef.value,
        url: `/store.orderRefund/audit`,
        list: list
    })
}

const refundAddressChange = ({value, row}:any) => {
    edit.info.refundAddress = row
    edit.info.refund_address_id = row.id
}

</script>

<style scoped>

</style>
